package com.cwvs.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.cwvs.helper.UrlHelper;
import com.cwvs.utils.MySqlPageUtil;
import com.cwvs.utils.Pages;

/**
 * 
 * 科室和科室专家
 */
@Repository
public class AppDepartmentDao {
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private MySqlPageUtil mySqlPageUtil;
	@Resource
	private UrlHelper urlHelper;

	/**
	 * 科室列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> departmentList(String store_id) {
		if(StringUtils.isBlank(store_id)){
			return new ArrayList<Map<String, Object>>();
		}
		StringBuffer sbf = new StringBuffer();
		sbf.append("SELECT ID,NAME FROM APP_STORE_DEPARTMENT WHERE is_active=1 ");
		List<Object> params = new ArrayList<Object>();
		if (StringUtils.isNotBlank(store_id)) {
			sbf.append(" AND STORE_ID=? ");
			params.add(store_id);
		}
		return jdbcTemplate.queryForList(sbf.toString(), params.toArray());
	}

	/**
	 * 专家列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> personList(String department_id) {
		if(StringUtils.isBlank(department_id)){
			return new ArrayList<Map<String, Object>>();
		}
		StringBuffer sbf = new StringBuffer();
		sbf.append("SELECT ID,NAME,PHONE FROM APP_STORE_DEPARTMENT_PERSON WHERE is_active=1 ");
		List<Object> params = new ArrayList<Object>();
		if (StringUtils.isNotBlank(department_id)) {
			sbf.append(" AND DEPARTMENT_ID=? ");
			params.add(department_id);
		}
		return jdbcTemplate.queryForList(sbf.toString(), params.toArray());
	}
	
	/**
	 * 通过ID获取科室信息
	 */
	public Map<String,Object> getDepartmentInfo(String id){
		List<Map<String,Object>> ls=jdbcTemplate.queryForList(" SELECT ID,NAME,IS_ACTIVE,STORE_ID,CTIME,UTIME FROM APP_STORE_DEPARTMENT WHERE ID= ?",new Object[]{id});
		if(!ls.isEmpty()){
			return ls.get(0);
		}
		return new HashMap<String, Object>();
	}
	/**
	 * 通过ID获取专家信息
	 */
	public Map<String,Object> getPersonInfo(String id){
		List<Map<String,Object>> ls=jdbcTemplate.queryForList(" SELECT ID,NAME,IS_ACTIVE,DEPARTMENT_ID,CTIME FROM APP_STORE_DEPARTMENT_PERSON WHERE ID= ?",new Object[]{id});
		if(!ls.isEmpty()){
			return ls.get(0);
		}
		return new HashMap<String, Object>();
	}
	
	/**
	 * 科室列表
	 * 
	 * @return
	 */
	public Pages departmentList(Map<String, Object> m, Pages page) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT T1.ID,T1.NAME,T1.CTIME,T1.UTIME,T1.IS_ACTIVE,T2.NAME STORE_NAME ");
		sql.append(" FROM APP_STORE_DEPARTMENT T1  ");
		sql.append(" LEFT JOIN APP_STORE T2 ON T1.STORE_ID=T2.ID ");
		sql.append("  WHERE 1=1  ");
		List<Object> params = new ArrayList<Object>();
		if(StringUtils.isNotBlank(MapUtils.getString(m, "NAME"))){
			sql.append(" AND UPPER(T1.NAME) LIKE ? ");
			params.add("%"+MapUtils.getString(m, "NAME")+"%");
		}
		sql.append(" ORDER BY T1.UTIME DESC ");
		String page_sql = mySqlPageUtil.getPageSql(page, sql.toString());
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(page_sql,
				params.toArray());
		page.setPage(ls);
		page.setTotalRecord(departmentCount(m));
		return page;
	}
	private int departmentCount(Map<String,Object> m){
		StringBuffer sql=new StringBuffer();
		sql.append(" SELECT COUNT(1) FROM APP_STORE_DEPARTMENT T1 WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		if(StringUtils.isNotBlank(MapUtils.getString(m, "NAME"))){
			sql.append(" AND UPPER(T1.NAME) LIKE ? ");
			params.add("%"+MapUtils.getString(m, "NAME")+"%");
		}
		return jdbcTemplate.queryForInt(sql.toString(),params.toArray());
	}
	
	/**
	 * 保存或者修改科室
	 */
	public boolean saveOrUpdate(Map<String, Object> o) {
		if (StringUtils.isBlank(MapUtils.getString(o, "ID"))) {
			StringBuffer sbf = new StringBuffer();
			String id = UUID.randomUUID().toString();
			sbf.append(" INSERT INTO APP_STORE_DEPARTMENT(");
			sbf.append("ID,NAME,STORE_ID ,CTIME,UTIME)VALUES(");
			sbf.append("?,?,?,?,?)");
			List<Object> ls = new ArrayList<Object>();
			ls.add(id);
			ls.add(MapUtils.getString(o, "NAME"));
			ls.add(MapUtils.getString(o, "STORE_ID"));
			ls.add(new Date());
			ls.add(new Date());
			return jdbcTemplate.update(sbf.toString(), ls.toArray()) > 0;
		} else {
			StringBuffer sbf = new StringBuffer();
			sbf.append(" UPDATE APP_STORE_DEPARTMENT SET NAME=? ");
			sbf.append(" ,UTIME=? ");
			sbf.append(" WHERE ID=?  ");
			List<Object> ls = new ArrayList<Object>();
			ls.add(MapUtils.getString(o, "NAME"));
			ls.add(new Date());
			ls.add(MapUtils.getString(o, "ID"));
			return jdbcTemplate.update(sbf.toString(), ls.toArray()) > 0;
		}

	}
	
	/**
	 * 状态更新
	 * @param id
	 * @return
	 */
	public boolean departmentSetStaus(String id,boolean status){
		return jdbcTemplate.update("update APP_STORE_DEPARTMENT set is_active=?,utime=?  where id=? ",new Object[]{status,new Date(),id})>0;
	}
}
